package com.baidu.ueditor.hunter;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.MultiState;
import com.baidu.ueditor.define.State;

public class FileManager {
    private int count;
    private int total;

    public static String endpoint;
    public static String accessKeyId;
    public static String accessKeySecret;
    public static String baseUrl;
    public static String bucket;
    public static String uploadDirPrefix;
    public static int pageSize;

    public FileManager(Map<String, Object> conf) {
        this.count = (Integer) conf.get("count");

    }

    public State listFile(int index, String marker) {
        State state;
        OSSClient ossClient = null;
        try {
            ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            // 列举Object
            ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(bucket)
                    .withMarker(marker).withMaxKeys(pageSize).withEncodingType("url"));
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            List<String> fileList = new ArrayList<>();
            for (OSSObjectSummary s : sums) {
                fileList.add(s.getKey());
            }
            state = this.getState(fileList.toArray(new String[fileList.size()]));
            state.putInfo("start", index);
            state.putInfo("isLast", (sums.size() <= 0) + "");
            state.putInfo("marker", objectListing.getNextMarker());
            state.putInfo("total", Integer.MAX_VALUE);
        } catch (OSSException oe) {
            state = new BaseState(false, AppInfo.NOT_EXIST);
            state.putInfo("code", oe.getErrorCode());
            state.putInfo("message", oe.getMessage());
        } catch (ClientException ce) {
            state = new BaseState(false, AppInfo.IO_ERROR);
            state.putInfo("code", ce.getErrorCode());
            state.putInfo("message", ce.getMessage());
        } finally {
            // 关闭client
            if (ossClient != null) ossClient.shutdown();
        }
        return state;
    }

    public State deleteFile(String key) {
        State state;
        OSSClient ossClient = null;
        try {
            ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            // 删除单个Object
            ossClient.deleteObject(bucket, key);
            state = new BaseState(true);

        } catch (OSSException oe) {
            state = new BaseState(false, AppInfo.IO_ERROR);
            state.putInfo("code", oe.getErrorCode());
            state.putInfo("message", oe.getMessage());
        } catch (ClientException ce) {
            state = new BaseState(false, AppInfo.IO_ERROR);
            state.putInfo("code", ce.getErrorCode());
            state.putInfo("message", ce.getMessage());
        } finally {
            // 关闭client
            if (ossClient != null) ossClient.shutdown();
        }
        return state;
    }

    private State getState(String[] files) {
        MultiState state = new MultiState(true);
        BaseState fileState;

        for (String url : files) {
            if (url == null) break;
            fileState = new BaseState(true);
            fileState.putInfo("url", baseUrl + "/" + url);
            state.addState(fileState);
        }

        return state;
    }
}
